package com.example.lenovo.downloaddemo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lenovo.downloaddemo.R;
import com.example.lenovo.downloaddemo.bean.RankInfoBean;
import com.example.lenovo.downloaddemo.http.AppConfig;
import com.example.lenovo.downloaddemo.http.okhttp.utils.AppContext;
import com.example.lenovo.downloaddemo.http.okhttp.utils.CommonUtils;
import com.example.lenovo.downloaddemo.util.TextStyleLanguage;
import com.squareup.picasso.Picasso;

import java.util.List;

public class RankInfoAdapter extends BaseAdapter {

    private Context context;
    private List<RankInfoBean.DataBean.GoodsListBean> data;


    public RankInfoAdapter(Context context,List<RankInfoBean.DataBean.GoodsListBean> data){
        this.context=context;
        this.data=data;
    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(view==null){
            holder=new ViewHolder();
            view=LayoutInflater.from(AppContext.getInstance()).inflate(R.layout.rankinfo_item,null);
            holder.iv_icon=(ImageView) view.findViewById(R.id.iv_icon);
            holder.tv_name=(TextView) view.findViewById(R.id.product_name);
            holder.download_num=(TextView) view.findViewById(R.id.product_download_num);
            holder.linlayour=(LinearLayout) view.findViewById(R.id.linlayour);
            view.setTag(holder);

        }else{
            holder= (ViewHolder) view.getTag();
        }
        if(!TextStyleLanguage.isEngLish()){
            holder.tv_name.setText(data.get(i).getGood_name());
        }else{
            holder.tv_name.setText(data.get(i).getGood_name_en());
        }


        holder.download_num.setText(AppContext.getInstance().getResources().getText(R.string.download_num)+"："+data.get(i).getDownload_num()+"");
        int width = (CommonUtils.getScreenWidth(context))/5;
        int height=CommonUtils.getScreenHeight(context)/15;

        int w=(CommonUtils.getScreenWidth(context));
        int h=(CommonUtils.getScreenHeight(context))/10;
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, h);
        LinearLayout.LayoutParams l = new LinearLayout.LayoutParams(w, h);
        holder.iv_icon.setScaleType(ImageView.ScaleType.FIT_XY);
        holder.iv_icon.setLayoutParams(lp);
        holder.linlayour.setLayoutParams(l);
        Picasso.with(context)
                .load(AppConfig.IP+data.get(i).getGood_pic())
                .config(Bitmap.Config.RGB_565)

                .into(holder.iv_icon);








        return view;
    }

    static  class ViewHolder{
        ImageView iv_icon;//产品图标
        TextView tv_name;//产品名称
        TextView download_num;//下载数量
        LinearLayout linlayour;
    }

}
